<?php

$pid_file_name = "/var/run/smsbridge.pid";
$bin_path = "smsbridge";
$stat_file = "/usr/local/etc/smsbridge/stat.html";
$pid = false;

function getSessions($stat_file) {
  if(file_exists($stat_file))
    echo file_get_contents($stat_file);
}

function getLog($log_file) {
  $res = "<table><tr><th>Time</th><th>Application</th><th>Class</th><th>Object</th><th>Message</th></tr>";
  if(file_exists($log_file)) {
    $data = file_get_contents($log_file);
    $file_size = strlen($data);
    if($file_size > 0) {
      $pos = 0;
      $avail_bytes = $file_size - $pos;
      while($avail_bytes > 0) {
        if($avail_bytes < 8) {
          break;
        }
        $hi = current(unpack('N', substr($data, $pos, 4)));
        $pos += 4;
        $lo = current(unpack('N', substr($data, $pos, 4)));
        $pos += 4;
        $r = (($lo >> 16) + ($hi << 16));
        $ans = ($r / 1000);
        $r = (($r % 1000) << 16) + ($lo & 0xFFFF);
        $ans = ($ans << 16) + $r / 1000;      
        $timestamp = date("d-m-Y H:m:s", $ans);
        $avail_bytes = $file_size - $pos;
        if($avail_bytes < 1) {
          break;
        }
        $log_level = current(unpack('C', substr($data, $pos, 1)));
        switch($log_level) {
          case 1:
            $log_level = "fatal";
            break;
          case 2:
            $log_level = "error";
            break;          
          case 3:
            $log_level = "warning";
            break;          
          case 4:
            $log_level = "info";
            break;           
          case 5:
            $log_level = "debug";
            break;           
          default:
            $log_level = "warning";
            break;             
        }
        $pos ++;
        $avail_bytes = $file_size - $pos;
        if($avail_bytes < 4) {
          break;
        }        
        $app = current(unpack('N', substr($data, $pos, 4)));
        if($app == 2) {
          $app = "SMSBridge";
        }
        $pos += 4;
        $avail_bytes = $file_size - $pos;
        if($avail_bytes < 4) {
          break;
        } 
        $cls = current(unpack('N', substr($data, $pos, 4)));
        if($cls == 1) {
          $cls = "UnixDaemon";
        } else if($cls == 2) {
          $cls = "Kernel";
        }        
        $pos += 4;
        $avail_bytes = $file_size - $pos;
        if($avail_bytes < 4) {
          break;
        }
        $obj = current(unpack('N', substr($data, $pos, 4)));
        if($obj == 1) {
          $obj = "UnixDaemon";
        } else if($obj == 2) {
          $obj = "Kernel";
        } 
        $pos += 4;
        $avail_bytes = $file_size - $pos;
        if($avail_bytes < 4) {
          break;
        }
        $msg_size = current(unpack('N', substr($data, $pos, 4)));
        $pos += 4;
        $avail_bytes = $file_size - $pos;        
        if($avail_bytes < $msg_size) {
          break;
        }
        $msg = substr($data, $pos, $msg_size);
        $pos += $msg_size;
        $avail_bytes = $file_size - $pos;
        $res .= "<tr class=\"" . $log_level . "\">" .
                "<td>" . $timestamp . "</td>" .
                "<td>" . $app . "</td>" .
                "<td>" . $cls . "</td>" .
                "<td>" . $obj . "</td>" .
                "<td>" . $msg . "</td></tr>";
      }
    }
    
  }
  $res .= "</table>";
  echo $res;
}

function getProcessInfo($pid) {
  $ps_info = array();

  if($pid) {
    $ps_out = null;

    exec("sudo ps -aux -p $pid", $ps_out);

    if(count($ps_out) == 2) {
      $ps_arr = preg_split('/\s+/', $ps_out[1]);

      $ps_info["pid"]         = $ps_arr[1];
      $ps_info["cpu_percent"] = $ps_arr[2];
      $ps_info["mem_percent"] = $ps_arr[3];
      $ps_info["mem_virtual"] = $ps_arr[4];
      $ps_info["mem_page"]    = $ps_arr[5];
      $ps_info["start_time"]  = $ps_arr[8];
      $ps_info["command"]     = $ps_arr[10];
    }
  }

  echo json_encode($ps_info);
}

function startProcess($pid, $proc) {
  if(!$pid)
    exec("sudo $proc");
}

function stopProcess($pid) {
  if($pid)
    exec("sudo kill $pid");
}

if(isset($_GET["c"])) {
  $cmd = $_GET["c"];
  
  if(file_exists($pid_file_name)) {
    $pid = exec("sudo cat $pid_file_name");
    if(strlen($pid) <= 1)
      $pid = false;
  }  
  
  if(strcmp($cmd, "ps") == 0) {
    getProcessInfo($pid);
  } else if(strcmp($cmd, "start") == 0) {
    startProcess($pid, $bin_path);
  } else if(strcmp($cmd, "stop") == 0) {
    stopProcess($pid);
  } else if(strcmp($cmd, "sessions") == 0) {
    getSessions($stat_file);
  } else if(strcmp($cmd, "log") == 0) {
    $date = null;
    if(isset($_GET["date"])) {
      list($year, $month, $day) = split("-", $_GET["date"]);
      if($day && $month && year) {
        $date = $day . "_" . $month . "_" . $year;
      }
    }
    if($date != null) {
      $log_file = "/var/log/smsbridge_" . $date . ".log";
      getLog($log_file);
    }
  }
}


